define(
    function (require) {
        require('tpl!./list.tpl.html');

        var util = require('common/util');
        var u = require('underscore');
    
        var showid = 1;
        
        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 20,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '商品名称',
                field: 'name' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return [
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="modify">'
                            + item.productName
                            + '</a>'
                    ].join('');
                }
            },
            {
                title: '商品图片',
                field: 'titlePic' ,
                resizable : true,
                width: 120,
                content: function (item) {
                    return [
                        '<img src="' + item.pic + '" width="100" />'
                    ].join('');
                }
            },
            {
                title: '数量',
                field: 'price' ,
                width: 100,
                content: function (item) {
                    return item.amount;
                }
            },
            {
                title: '二维码',
                field: 'qrcode' ,
                resizable : true,
                width: 120,
                content: function (item) {
                    return [
                        '<img src="' + item.qrcode + '" width="100" />'
                    ].join('');
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 60,
                content: function (item) {
                    var operates = [
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="modify">修改</a></span>',
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="delete">删除</a></span>'
                    ];
                    return operates.join('');
                }
            }
        ];

        function search() {
            var shopid = this.get('surprise-shop-id');
            this.fire('search', { shopid: shopid.getValue()});
        }
        
        var BaseListView = require('common/ListView');

        function ListView() {
            BaseListView.apply(this, arguments);
        }

        ListView.prototype.template = 'surpriseProductListPage';

        ListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);
            showid = 1;
            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无商品信息!</p>'
                }
            };
        }


        ListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('searchButton').on(
                'click',
                util.bind(search, this)
            );
            
            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );

            //this.get('list').initChildren();
        }

        require('er/util').inherits(ListView, BaseListView);
        return ListView;
    }
);
